package com.zx.mobile.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
import com.zx.map.dao.model.Employee;
import com.zx.map.service.EmployeeService;
import com.zx.map.utils.CommonResponseUtils;
import com.zx.mobile.domain.MobileUserDomain;
import com.zx.mobile.utils.WebUtils;
import com.zx.platform.utils.PmpUtils;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

	@Autowired
	private EmployeeService employeeService;

	@RequestMapping("/add")
	@ResponseBody
	public Map add(@RequestBody Employee employee, HttpServletRequest request) {

		try {
			MobileUserDomain mUserDomain = WebUtils.queryCurrentLoginInfo(request);
			String merchantId = mUserDomain.getMerchantInfoDetailModel().getMerchantId();
			employee.setPassword(PmpUtils.encoderByMD5(employee.getPassword()));
			employee.setMerchantid(merchantId);
			employeeService.add(employee);
		} catch (Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}

		return CommonResponseUtils.successResponse();
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Map delete(@RequestBody Employee employee, HttpServletRequest request) {

		try {

			employeeService.delete(employee.getId());
		} catch (Throwable e) { 
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}

		return CommonResponseUtils.successResponse();
	}

	@RequestMapping("/passwordreset")
	@ResponseBody
	public Map passwordreset(@RequestBody Employee employee, HttpServletRequest request) {
		int num=(int) (Math.random() * 1000000);
		String password=String.valueOf(num);
		try {
			employee.setPassword(PmpUtils.encoderByMD5(password+"bs"));
			employeeService.updatePassword(employee);
		} catch (Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}
		Map m = new HashMap();
		m.put("password", password+"bs");
		return CommonResponseUtils.successResponse("ok",m);
	}

	@RequestMapping("/list")
	@ResponseBody
	public Map list(HttpServletRequest request) {

		try {
			MobileUserDomain mUserDomain = WebUtils.queryCurrentLoginInfo(request);
			List<Employee> list = employeeService.list(mUserDomain.getMerchantInfoDetailModel().getMerchantId());
			return CommonResponseUtils.successResponse(list);
		} catch (Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}

	}

	@RequestMapping("/gettype")
	@ResponseBody
	public Map gettype(Integer id, HttpServletRequest request) {
		try {
			Employee e = employeeService.get(id);
			return CommonResponseUtils.successResponse(e);
		} catch (Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}

		
	
	}

}
